<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class system extends CI_Controller{
        private $roomGrade; 
        private $money;
        private $gameSpeed;
        private $roomconf;
	public function __construct(){
		parent::__construct();
		if(!isset($_SESSION["isadmin"]) || !$_SESSION["isadmin"]){
			header("location:/index.php/login");
		}
                $this->roomGrade = $this->config->item('room_grade');
                $this->money = $this->config->item('money');
                $this->gameSpeed = $this->config->item('room_gameSpeed');
                $this->roomconf = $this->config->item('roomconf');
	}
	public function conf(){
                $this->load->model('SystemModel');
                $sql = " SELECT * FROM pk_system ";
                $data['conf'] = $this->SystemModel->getConf($sql);
                
		$this->load->view("header");
		$this->load->view("system/conf",$data);
		$this->load->view("footer");
	}
	
	public function confEdit(){
                $this->load->model('SystemModel');
                $sql = " SELECT * FROM pk_system ";
                $data['conf'] = $this->SystemModel->getConf($sql);
                
		$this->load->view("header");
		$this->load->view("system/confEdit",$data);
		$this->load->view("footer");
	}
        
        public function confSave(){
            $set = array();
            $set['qq_api_state'] = $_GET['qq_api_state'];
            $set['qq_api_key'] = $_GET['qq_api_key'];
            $set['sina_api_state'] = $_GET['sina_api_state'];
            $set['sina_api_key'] = $_GET['sina_api_key'];
            
            $this->load->model('SystemModel');
            foreach($set as $k=>$v ){
                $sql = " UPDATE pk_system SET  value = '{$v}' WHERE name ='{$k}' ";
                $this->SystemModel->sqlquery($sql); 
            }
            mcmessage_error('修改配置成功!', '/index.php?c=system&a=conf');
        }
        
        public function room(){
                $url = "/index.php?c=system&a=room";
                $roomID = isset( $_GET['roomID']) && 0 < intval($_GET['roomID']) ? intval($_GET['roomID']) : '';
                $data['roomID'] = $roomID;
                $where = "";
                if( $roomID ){
                    $where  = " WHERE id = '{$roomID}'" ;
                    $url.= "&roomID=".$roomID;
                }

                $this->config->load('pagination',true);
                $pagesize  = $this->config->item("per_page","pagination");
                $sposition  = isset($_GET['per_page']) && (int)$_GET['per_page'] ? (int)$_GET['per_page'] : 0;
                $limit .= " LIMIT $sposition ,$pagesize ";
                
                $this->load->model('SystemModel');
                $sql = " SELECT * FROM pk_room ".$where.$limit;
                $data['roomList'] = $this->SystemModel->getAll($sql);
                $data['grade'] = $this->roomGrade; 
                $data['money'] = $this->money;
                $data['speed'] = $this->gameSpeed;
                 
                $this->load->library('pagination');
                $config['base_url'] = $url;
                $config['total_rows'] = $this->SystemModel->roomCount($where);
                $this->pagination->initialize($config);
                $data['page'] = $this->pagination->create_links();
                
            
            
                $this->load->view("header");
		$this->load->view("system/room",$data);
		$this->load->view("footer");
        }
        
        public function roomAdd(){
            $data['grade'] = $this->roomGrade;
            $data['money'] = $this->money;
            $data['speed'] = $this->gameSpeed;
            $data['roomconf'] = $this->roomconf;
            $this->load->view("header");
            $this->load->view("system/roomEdit",$data);
            $this->load->view("footer");
        }
        
        public function roomEdit(){
            $roomID = isset($_GET['roomID'])?intval($_GET['roomID']):'';
            if( !$roomID ){
                mcmessage_error('出错了，请重试！', '/index.php?c=system&a=room');
            }
            $data['roomID'] = $roomID;
            $this->load->model('SystemModel');
            $sql = " SELECT * FROM pk_room WHERE id= '{$roomID}' limit 1";
            $room = $this->SystemModel->getRow($sql);
            $room['condition'] = unserialize($room['condition']);
            $data['room'] = $room;
            $data['grade'] = $this->roomGrade;
            $data['money'] = $this->money;
            $data['speed'] = $this->gameSpeed;
            $data['roomconf'] = $this->roomconf;
            $this->load->view("header");
            $this->load->view("system/roomEdit",$data);
            $this->load->view("footer");
        }
        
        public function roomSave(){
            $set = array();
            if(!empty($_GET['title'])){
                $set[] = "title = '{$_GET['title']}'";
            }
            $set[] = "roomGrade = '{$_GET['roomGrade']}'";

            if( 0 < intval($_GET['roomMaxDesk'])){
                $set[] = "roomMaxDesk = '".intval($_GET['roomMaxDesk'])."'";
            }
            if( 0 < intval($_GET['roomMaxNO'])){
                $set[] = "roomMaxNO = '".intval($_GET['roomMaxNO'])."'";
            }
            if( 0 < intval($_GET['addExp'])){
                $set[] = "addExp = '".intval($_GET['addExp'])."'";
            }
            if( 0 < intval($_GET['lBlind'])){
                $set[] = "lBlind = '".intval($_GET['lBlind'])."'";
            }
            if( 0 < intval($_GET['bBlind'])){
                $set[] = "bBlind = '".intval($_GET['bBlind'])."'";
            }
            if( 0 < intval($_GET['lGameMoney'])){
                $set[] = "lGameMoney = '".intval($_GET['lGameMoney'])."'";
            }
            if( 0 < intval($_GET['BGameMoney'])){
                $set[] = "BGameMoney = '".intval($_GET['BGameMoney'])."'";
            }
            if( 0 < intval($_GET['DeskMaxHuman'])){
                $set[] = "DeskMaxHuman = '".intval($_GET['DeskMaxHuman'])."'";
            }
            if( 0 <= intval($_GET['gameSpeed'])){
                $set[] = "gameSpeed = '".intval($_GET['gameSpeed'])."'";
            }
            $condition = array();
            if( 0 < intval($_GET['grade']) ){
                $condition['grade'] = intval($_GET['grade']);
            }
            if( 0 < intval($_GET['money']) ){
                $condition['money'] = intval($_GET['money']);
            }
            if( 0 < intval($_GET['jetton']) ){
                $condition['jetton'] = intval($_GET['jetton']);
            }
            if( 0 < intval($_GET['victory']) ){
                $condition['victory'] = intval($_GET['victory']);
            }
            if( 0 < intval($_GET['maxPayNO']) ){
                $condition['maxPayNO'] = intval($_GET['maxPayNO']);
            }
            $set[] = "`condition`= '".serialize($condition)."'";
            $this->load->model('SystemModel');
            if(isset($_GET['roomID']) && 0 < intval($_GET['roomID']) ){
                $sql = "UPDATE pk_room SET ".  implode( ',',$set)." WHERE id='".intval($_GET['roomID'])."'" ;
                $message = "修改房间";
            }else{
                $set[] = "ctime = '".time()."'";
                $sql =" INSERT INTO pk_room SET ".  implode( ',',$set);
                $message = "添加房间";
            }
            if( $this->SystemModel->sqlquery($sql)){
                if(isset($_GET['roomID']) && 0 < intval($_GET['roomID']) ) {
                        $this->SystemModel->AddRoomToGame();
                }
                mcmessage_error($message.'成功!', '/index.php?c=system&a=room');
            }else{
                mcmessage_error($message.'失败!', '/index.php?c=system&a=room');
            }
        }
        
        public function roomState(){
            $roomID = isset($_GET['roomID']) && 0 < intval($_GET['roomID'] )? intval($_GET['roomID'] ) : '';
            if( !$roomID ){
                mcmessage_error('出错了，请重试！', '/index.php?c=system&a=room');
            }
            if( 1 == $_GET['state'] ){
                $message = "禁用房间";
            }else{
                $message = "启用房间";
            }
            $this->load->model('SystemModel');
            $sql = " UPDATE pk_room SET isDel = '". intval($_GET['state']) ."' WHERE id = '{$roomID}'"; 
            if( $this->SystemModel->sqlquery($sql)){
                mcmessage_error($message.'成功!', '/index.php?c=system&a=room');
            }else{
                mcmessage_error($message.'失败!', '/index.php?c=system&a=room');
            }
        }
        
        public function grade(){
                $url = "/index.php?c=system&a=grade";
                
                $this->config->load('pagination',true);
                $pagesize  = $this->config->item("per_page","pagination");
                $sposition  = isset($_GET['per_page']) && (int)$_GET['per_page'] ? (int)$_GET['per_page'] : 0;
                $where .= " LIMIT $sposition ,$pagesize ";
                
                $this->load->model('SystemModel');
                $sql = " SELECT * FROM pk_system_grade ".$where;
                $data['gradeList'] = $this->SystemModel->getAll($sql);
                $data['money'] = $this->money;    
                
                $this->load->library('pagination');
                $config['base_url'] = $url;
                $config['total_rows'] = $this->SystemModel->gradeCount();
                $this->pagination->initialize($config);
                $data['page'] = $this->pagination->create_links();
            
                $this->load->view("header");
		$this->load->view("system/grade",$data);
		$this->load->view("footer");
        }
        
        public function gradeAdd(){
                $data['money'] = $this->money;
                $this->load->view("header");
		$this->load->view("system/gradeEdit",$data);
		$this->load->view("footer");
        }
        
        public function gradeSave(){
            $gradeID = intval($_GET['gradeID']) ?  intval($_GET['gradeID']) : '';
            $set = array();
            if( isset($_GET['grade']) && 0 < intval($_GET['grade']) ){
                $set[] = "grade = '".intval($_GET['grade'])."'";
            }
            if( isset($_GET['exp']) && 0 < intval($_GET['exp']) ){
                $set[] = "exp = '".intval($_GET['exp'])."'";
            }
            if( isset($_GET['award']) && 0 < intval($_GET['award']) ){
                $set[] = "award = '".intval($_GET['award'])."'";
            }
            
            if( 0 == count($set) ){
                mcmessage_error('出错了，请重试', $_SERVER['HTTP_REFERER']);exit;
            }
            $this->load->model('SystemModel');
            
            if( $gradeID ){
                $sql = " UPDATE pk_system_grade SET ".implode(',', $set)." WHERE id = '{$gradeID}'";
                $message = "修改";
            }else{
                $set[] = "ctime ='".time()."' ";
                $sql = "INSERT INTO pk_system_grade SET ".implode(',', $set);
                $message = "添加";
            }
            
            if( $this->SystemModel->sqlquery($sql)){
                mcmessage_error($message.'成功!', '/index.php?c=system&a=grade');
            }else{
                mcmessage_error($message.'失败!', '/index.php?c=system&a=grade');
            }
            
        }
        
        public function gradeEdit(){
                $gradeID = intval($_GET['gradeID']) ?  intval($_GET['gradeID']) : '';
                if( !$gradeID ){
                    mcmessage_error('出错了，请重试!','/index.php?c=system&a=grade');exit;
                }
                $data['gradeID'] = $gradeID;
                $this->load->model('SystemModel');
                $sql = " SELECT * FROM pk_system_grade WHERE id = '{$gradeID}'";
                $data['grade'] = $this->SystemModel->getRow($sql);
                $data['money'] = $this->money;
                $this->load->view("header");
		$this->load->view("system/gradeEdit",$data);
		$this->load->view("footer");
        }
        
        public function gradeDel(){
                $gradeID = intval($_GET['gradeID']) ?  intval($_GET['gradeID']) : '';
                if( !$gradeID ){
                    mcmessage_error('出错了，请重试!','/index.php?c=system&a=grade');exit;
                }
                
                $this->load->model('SystemModel');
                $sql = " DELETE FROM pk_system_grade WHERE id = '{$gradeID}'";
                if( $this->SystemModel->sqlquery($sql)){
                    mcmessage_error('删除成功!', '/index.php?c=system&a=grade');
                }else{
                    mcmessage_error('删除失败!', '/index.php?c=system&a=grade');
                }
        }
}